home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jrichtext.tcl < prev    next >
Encoding:
Text File  |  1995-02-05  |  8.4 KB  |  246 lines

  1. # jrichtext.tcl - procedures for dealing with rich text
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non¡profit, noncommercial use.
  5. ######################################################################
  6.  
  7. # CHANGES:
  8. #   dual usage; j:rt:textfonts with a text widget vs. full rich-text
  9.  
  10. # j:tagged_insert w text args - insert tagged text into a text widget
  11. # j:rt text dest - prepare to write rich text to text widget dest
  12. # j:rt:type - return type of current rich text destination (text, TeX)
  13. # j:rt:destination - return current rich text destination (widget, file)
  14. # j:rt:textfonts {style font}... - set fonts for text widget
  15. # j:rt:done - finish writing rich text (clear vars, close files)
  16. # j:rt:rm text - write rich text (roman)
  17. # j:rt:it text - write rich text (italic)
  18. # j:rt:bf text - write rich text (bold face)
  19. # j:rt:bi text - write rich text (bisexual)
  20. # j:rt:tt text - write rich text (typewriter - monospaced)
  21. # j:rt:hl text - write rich text (`headline' - larger bold)
  22. # j:rt:tab - tab in rich text
  23. # j:rt:cr - line break in rich text
  24. # j:rt:par - paragraph break in rich text
  25. # j:rt:mkabbrevs - make shorter convenience procs, for text-intensive apps
  26. # rm - dummy do-nothing procedure to prevent unknown from calling /bin/rm
  27. #   if you forget to j:rt:mkabbrevs
  28.  
  29. ######################################################################
  30. # j:tagged_insert - append to a text widget with a particular tag
  31. #   (lifted from mkStyles.tcl demo, where it was insertWithTags)
  32. ######################################################################
  33.  
  34. # The procedure below inserts text into a given text widget and
  35. # applies one or more tags to that text.  The arguments are:
  36. #
  37. # w        Window in which to insert
  38. # text        Text to insert (it's inserted at the "insert" mark)
  39. # args        One or more tags to apply to text.  If this is empty
  40. #        then all tags are removed from the text.
  41.  
  42. proc j:tagged_insert {w text args} {
  43.   set start [$w index insert]
  44.   $w insert insert $text
  45.   foreach tag [$w tag names $start] {
  46.     $w tag remove $tag $start insert
  47.   }
  48.   foreach i $args {
  49.     $w tag add $i $start insert
  50.   }
  51. }
  52.  
  53. ######################################################################
  54. # j:rt text dest - prepare to write rich text to text widget dest
  55. #   future versions will support PostScript, TeX, maybe canvas, etc.
  56. ######################################################################
  57.  
  58. proc j:rt { {type {}} {destination stdout} } {
  59.   global j_rt
  60.   
  61.   case $type in {
  62.     {text} {            ;# output to a text widget
  63.       set j_rt(type) $type
  64.       set j_rt(destination) $destination
  65.       $j_rt(destination) delete 0.0 end
  66.       $j_rt(destination) configure -wrap word
  67.       catch {
  68.         $j_rt(destination) configure -font \
  69.           -adobe-helvetica-medium-r-normal--*-120-*
  70.         $j_rt(destination) tag configure richtext:font:roman -font \
  71.           -adobe-helvetica-medium-r-normal--*-120-*
  72.         $j_rt(destination) tag configure richtext:font:italic -font \
  73.           -adobe-helvetica-medium-o-normal--*-120-*
  74.         $j_rt(destination) tag configure richtext:font:bold -font \
  75.           -adobe-helvetica-bold-r-normal--*-120-*
  76.         $j_rt(destination) tag configure richtext:font:bolditalic -font \
  77.           -adobe-helvetica-bold-o-normal--*-120-*
  78.         $j_rt(destination) tag configure richtext:font:typewriter -font \
  79.           -adobe-courier-medium-r-normal--*-120-*
  80.         $j_rt(destination) tag configure richtext:font:heading0 -font \
  81.           -adobe-helvetica-bold-o-normal--*-240-*
  82.         $j_rt(destination) tag configure richtext:font:heading1 -font \
  83.           -adobe-helvetica-bold-o-normal--*-180-*
  84.         $j_rt(destination) tag configure richtext:font:heading2 -font \
  85.           -adobe-helvetica-bold-o-normal--*-140-*
  86.         $j_rt(destination) tag configure richtext:font:heading3 -font \
  87.           -adobe-helvetica-bold-o-normal--*-120-*
  88.         $j_rt(destination) tag configure richtext:font:heading4 -font \
  89.           -adobe-helvetica-bold-o-normal--*-100-*
  90.         $j_rt(destination) tag configure richtext:font:heading5 -font \
  91.           -adobe-helvetica-bold-o-normal--*-80-*
  92.       }
  93.     }
  94.     default {
  95.       tkerror "j:rt $type $destination: only type \"text\" is supported."
  96.     }
  97.   }
  98. }
  99.  
  100. ######################################################################
  101. # j:rt:textfonts w {{style fontlist}...} - set fonts for text widget w
  102. #   style is one of {roman italic bold bolditalic typewriter} or
  103. #   {heading0, ..., heading5}; font is list of X fonts, in order of
  104. #   decreasing preference (cf j:configure_tag_font in jtkutils.tcl).
  105. ######################################################################
  106.  
  107. proc j:rt:textfonts { w list } {
  108.   foreach pair $list {
  109.     set tag "richtext:font:[lindex $pair 0]"
  110.     set fontlist [lindex $pair 1]
  111.     j:configure_tag_font $w $tag $fontlist
  112.   }
  113. }
  114.  
  115. ######################################################################
  116. # j:rt:type - return type of current rich text destination (text, TeX)
  117. ######################################################################
  118.  
  119. proc j:rt:type {} {
  120.   global j_rt
  121.   
  122.   if { (! [info exists j_rt(type)])} {
  123.     # this might be considered an error
  124.     return {}
  125.   } else {
  126.     return $j_rt(type)
  127.   }
  128. }
  129.  
  130. ######################################################################
  131. # j:rt:destination - return current rich text destination (widget, file)
  132. ######################################################################
  133.  
  134. proc j:rt:destination {} {
  135.   global j_rt
  136.   
  137.   if { (! [info exists j_rt(destination)]) } {
  138.     # this might be considered an error
  139.     return {}
  140.   } else {
  141.     return $j_rt(destination)
  142.   }
  143. }
  144.  
  145. ######################################################################
  146. # j:rt:done - finish writing rich text (clear vars, close files)
  147. ######################################################################
  148.  
  149. proc j:rt:done {} {
  150.   global j_rt
  151.  
  152.   # to start, would close files if appropriate
  153.   
  154.   set j_rt(type) {}
  155.   set j_rt(destination) {}
  156. }
  157.   
  158. ######################################################################
  159. # CREATE PROCEDURES FOR:
  160. # j:rt:rm text - write rich text (roman)
  161. # j:rt:it text - write rich text (italic)
  162. # j:rt:bf text - write rich text (bold face)
  163. # j:rt:bi text - write rich text (bisexual)
  164. # j:rt:tt text - write rich text (typewriter - monospaced)
  165. # j:rt:hl text - write rich text (`headline' - larger bold)
  166. ######################################################################
  167.  
  168. set tmp_body {
  169.   set type [j:rt:type]
  170.   
  171.   case $type in {
  172.     {text} {            ;# output to a text widget
  173.       j:tagged_insert [j:rt:destination] $text $tag
  174.     }
  175.     default {
  176.       tkerror "j:rt type \"$type\" is not supported."
  177.     }
  178.   }
  179. }
  180.  
  181. foreach pair {
  182.   {rm roman}
  183.   {it italic}
  184.   {bf bold}
  185.   {bi bolditalic}
  186.   {tt typewriter}
  187.   {hl heading1}
  188.   {h0 heading0}
  189.   {h1 heading1}
  190.   {h2 heading2}
  191.   {h3 heading3}
  192.   {h4 heading4}
  193.   {h5 heading5}
  194. } {
  195.   set command [lindex $pair 0]
  196.   set style [lindex $pair 1]
  197.   proc j:rt:$command {text} "  set tag richtext:font:$style\n$tmp_body"
  198. }
  199.  
  200. ######################################################################
  201. # j:rt:tab - tab in rich text
  202. ######################################################################
  203.  
  204. proc j:rt:tab {} {
  205.   j:rt:rm "\t"
  206. }
  207.  
  208. ######################################################################
  209. # j:rt:cr - line break in rich text
  210. ######################################################################
  211.  
  212. proc j:rt:cr {} {
  213.   j:rt:rm "\n"
  214. }
  215.  
  216. ######################################################################
  217. # j:rt:par - paragraph break in rich text
  218. ######################################################################
  219.  
  220. proc j:rt:par {} {
  221.   j:rt:rm "\n\n"
  222. }
  223.  
  224. ######################################################################
  225. # j:rt:mkabbrevs - make shorter convenience procs, for text-intensive apps
  226. ######################################################################
  227.  
  228. # this creates shorter aliases rm, it, bf, bi, tt, hl, tab, cr, and
  229. # par identical to the corresponding procedures starting with "j:rt:"
  230.  
  231. proc j:rt:mkabbrevs {} {
  232.   foreach proc {rm it bf bi tt hl tab cr par} {
  233.     proc $proc [info args j:rt:$proc] [info body j:rt:$proc]
  234.   }
  235. }
  236.  
  237. ######################################################################
  238. # rm - dummy do-nothing procedure to prevent unknown from calling /bin/rm
  239. #   if you forget to j:rt:mkabbrevs
  240. ######################################################################
  241.  
  242. proc rm {args} {
  243.   tkerror "Called `rm' without calling `j:rt:mkabbrevs'."
  244. }
  245.